<?php

// Check to ensure this file is included in Joomla!
defined('_JEXEC') or die( 'Restricted access' ); 
jimport('joomla.html.html');
jimport('joomla.access.access');
jimport('joomla.form.formfield'); 
class JFormFieldtabselection extends JFormField
{
/**
* Element name
*
* @access	protected
* @var		string
*/
protected $type = 'tabselection';


protected function getInput(){ 
if (! @$class) {
$class = "inputbox";
}
$db = &JFactory::getDBO();
$document 	= JFactory::getDocument(); 
$document->addStylesheet(JURI::root() . 'modules/mod_sj_tabs/admin/css/adminstyle.css');
$document->addScript(JURI::root() . 'modules/mod_sj_tabs/admin/js/sj_dropdrap.js');
JHtml::_('behavior.modal', 'a.modal');
$url = str_replace('administrator/','',JURI::base()); 
$cId = JRequest::getVar('cid','');		 
if($cId !='') $cId = $cId[0];
if($cId == ''){
$cId = JRequest::getVar('id');
}
$db = JFactory::getDbo();
$query = 'SELECT DISTINCT position' 
. ' FROM #__modules AS a' 
. ' WHERE a.published = 1'
. ' AND a.position <> \'cpanel\'' 
. ' ORDER BY a.position';
$db->setQuery ( $query );
$db->getQuery ();
$options = $db->loadObjectList ();

$sql = "SELECT params FROM #__modules WHERE id=$cId";
$db->setQuery($sql);
$paramsConfigObj = $db->loadObjectList(); 
$db->setQuery($sql);
$data = $db->loadResult();
$params = new JParameter($data);
$text_data = $params->get('tabs_data');
$dataarray = explode('||', $text_data); 
$selection = array();  
$arySelection = array(); 
$baseURL = JURI::base(); 
$html_return = ' 
<div id="txselect"> 
<div id="sj_header">Your tabs:<span class="addslide">Add new tabs</span></div>
<div class="content-form" id="content-form">
<div class="height_scroll" style="height: 0px;">
<div class="itemsadd">
<div class="maintext">
<span class="title">'.JText::_( 'Title: ' ).'</span>
<input id="jform_title_tab" value="" name="jform_title_tab" size="20"/> 
</div>
<div class="maintext">
<span class="title">'.JText::_( 'Content Type: ' ).'</span>
<select name="jform_content_type_tab" id="jform_content_type_tab">
<option value="module">Module</option>
<option value="custom">Custom HTML</option>
</select>
</div>
<div class="maintext">
<span class="title">'.JText::_( 'Content: ' ).'</span>
<textarea id="jform_custom_content_tab" class="jform_custom_content_tab" aria-invalid="false" rows="7" cols="40"></textarea> 
</div>
<div class="maintext">
<span class="title">'.JText::_( 'Positions: ' ).'</span>
<select name="jform_positions_tab" id="jform_positions_tab">';
for($n = 0; $n < count ( $options ); $n ++) {
$html_return .='<option value="'.$options[$n]->position.'">'.$options[$n]->position.'</option>';
}
$html_return .='</select>
</div> 
<div class="maintext">
<div class="textbutton">
<span class="custombtn jform_save">'.JText::_( 'Save' ).'</span>
<span class="custombtn jform_cancel">'.JText::_( 'Cancel' ).'</span>
</div>
</div>
</div>
</div>
</div>
<div id="custom_list">';
$n=0;
foreach($dataarray as $item){
if($item){
$itemarray = explode('[]', $item);
$title = $itemarray[0];
$content_type = $itemarray[1];
if($content_type=='module'){
$tabdataposition = $itemarray[2];
}else{
$tabdatacontent = $itemarray[2];
} 
$html_return .= '
<div class="custom_item">
<div class="custom_item_preview">
<span class="custom_item_title">'.$title.'</span>
<span class="move" title="Move"></span>
<a class="remove" title="Remove" href="#remove"></a>
<a class="edit" title="Edit" href="#edit"></a>
<span class="type" title="Type">'.$content_type.'</span>
</div>
<div class="content-form-edit" id="content-form-edit" style="height: 0px;">
<div class="height_scroll">
<div class="itemsadd-edit">
<div class="maintext">
<span class="title">Title:</span>
<input id="jform_title_tab_'.$n.'" value="'.$title.'" name="jform_title_tab" size="20" > 
</div>
<div class="maintext">
<span class="title">Content Type:</span>
<select name="jform_content_type_tab" class="jform_content_type_tab" id="jform_content_type_tab_'.$n.'">
<option value="module">Module</option>
<option value="custom">Custom HTML</option>
</select>
</div>
<div class="maintext">
<span class="title">Content:</span>
<textarea id="jform_custom_content_tab_'.$n.'" class="jform_custom_content_tab" aria-invalid="false" rows="7" cols="40">'.@$tabdatacontent.'</textarea> 
</div>
<div class="maintext">
<span class="title">Position:</span>';
$arrOpt = array ();
for($i = 0; $i < count ( $options ); $i ++) { 
$arrOpt [$i] ['keys'] = $arrOpt [$i] ['value'] = $options [$i]->position;
}
$html_return .= JHTML::_ ( 'select.genericlist', $arrOpt, 'jform_positions_tab', 'id=jform_positions_tab_' . $n . '', 'keys', 'value', @$tabdataposition, 'jform_positions_tab_' . $n . '' );

$html_return .='</div>
<div class="maintext">
<div class="textbutton">
<span class="custombtn jform_save" onclick="upgradeItem('.$n.')">Save</span>
<span class="custombtn jform_cancel" onclick="cancelItem('.$n.')">Cancel</span>
</div>
</div>
</div>
</div>
<input type="hidden" value="'.$item.'" class="hiddendata" id="hiddendata_'.$n.'">
<input type="hidden" value="'.$n.'" class="numbsitem">
</div>
</div>
';
}
$n++;
} 
$html_return .= '</div>
</div>';
?>
<script type="text/javascript">
var $numbs = 0;
var $options = new Array();
<?php 
for($i = 0; $i < count ( $options ); $i ++) {
?>
$options[<?php echo $i;?>] = '<?php echo $options[$i]->position;?>';
<?php
}
?> 
window.addEvent('domready', function(){ 
$('jform_params_tabs_data').getParent().setStyle("display","none");
var customform = $('content-form'); 
var customform_btns = $$('div#content-form span.custombtn');
var scroll_wrap = customform.getElement('.height_scroll');
var fx = new Fx.Tween(scroll_wrap, { duration: 300, property: 'height', onComplete: function() { if(scroll_wrap.getSize().y > 0) scroll_wrap.setStyle('height', 'auto'); } });
document.id('sj_header').getElement('span').addEvent('click', function(e) {
e.stop();
fx.start(customform.getElement('.itemsadd').getSize().y);
});
customform_btns[1].addEvent('click', function(e) {
if(e) e.stop(); 
scroll_wrap.setStyle('height', scroll_wrap.getSize().y + 'px'); 
customform.getElement('#jform_title_tab').value="";
customform.getElement('#jform_custom_content_tab').value="";
customform.getElement('#jform_positions_tab').selectedIndex=0;
customform.getElement('#jform_content_type_tab').selectedIndex=1;
selectContentType('custom');
fx.start(0);
});
$('jform_content_type_tab').selectedIndex=1;
customform_btns[0].addEvent('click', function(e) {
addItem('new');
}); 
var selectContentType = function(type){
if(type == 'module'){
$('jform_custom_content_tab').getParent().setStyle('display','none');
$('jform_positions_tab').getParent().setStyle('display','');
}else if(type == 'custom'){
$('jform_custom_content_tab').getParent().setStyle('display','');
$('jform_positions_tab').getParent().setStyle('display','none');
}
}; 	 
$('jform_content_type_tab').addEvent('change',function(){
selectContentType(this.value);                
});
selectContentType('custom');
function addItem(source) {
var title = $('jform_title_tab').value;
var content_type = $('jform_content_type_tab').value;
var content = htmlspecialchars($('jform_custom_content_tab').value); 
var position = $('jform_positions_tab').value; 
if(source=='new'){
if(title!='' && content_type!=''){
var container = new Element('div',{'class':'custom_item'}).injectInside($('custom_list'));
var item_preview = new Element('div',{'class':'custom_item_preview'}).injectInside(container);
var span_title = new Element('span',{'class':'custom_item_title'}).set("html", title).injectInside(item_preview);
var span_down = new Element('span',{'class':'move', 'title':'Move'}).injectInside(item_preview); 
var a_rm = new Element('a',{'class':'remove', 'title':'Remove', 'href':'#remove'}).injectInside(item_preview);
var a_edit = new Element('a',{'class':'edit', 'title':'Edit', 'href':'#edit'}).injectInside(item_preview);
var type = new Element('span',{'class':'type', 'title':'Type'}).set("html",content_type).injectInside(item_preview);
var container_edit = new Element('div',{'class':'content-form-edit', 'id':'content-form-edit', 'style':'height:0px;'}).injectInside(container);
var container_height = new Element('div',{'class':'height_scroll'}).injectInside(container_edit);
var itemadd_edit = new Element('div',{'class':'itemsadd-edit'}).injectInside(container_height);
var maintext = new Element('div',{'class':'maintext'}).injectInside(itemadd_edit);
var spimg_title = new Element('span',{'class':'title'}).set("html", "Title:").injectInside(maintext);
var input_imp = new Element('input',{'value':title, 'id':'jform_title_tab_'+$numbs, 'name':'jform_title_tab', 'size':'20'}).injectInside(maintext);
//var button2 = new Element('div',{'class':'button2-left'}).injectInside(maintext);
//var button2_img = new Element('div',{'class':'image'}).injectInside(button2);
//var select_img = new Element('a',{'class':'modal', 'title':'Image', 'rel':'{handler: "iframe", size: {x: 800, y: 500}}', 'href':'<?php echo JURI::base();?>index.php?option=com_media&view=images&tmpl=component&fieldid=jform_img_path_'+$numbs+'&asset=&author='}).set("html","Image").injectInside(button2_img);
var maintext2 = new Element('div',{'class':'maintext'}).injectInside(itemadd_edit);
var sptitle_title = new Element('span',{'class':'title'}).set("html", "Content Type:").injectInside(maintext2);
var select_contenttype = new Element('select',{'id':'jform_content_type_tab_'+$numbs, 'name':'jform_content_type_tab'}).injectInside(maintext2);
var maintext3 = new Element('div',{'class':'maintext'}).injectInside(itemadd_edit);
var spcontent_title = new Element('span',{'class':'title'}).set("html", "Content:").injectInside(maintext3);
var text_content = new Element('textarea',{'value':htmlspecialchars_decode(content), 'id':'jform_custom_content_tab_'+$numbs, 'class':'jform_custom_content_tab', 'aria-invalid':'false', 'rows':'7', 'cols':'40'}).injectInside(maintext3);
var maintext4 = new Element('div',{'class':'maintext'}).injectInside(itemadd_edit);
var spurl_title = new Element('span',{'class':'title'}).set("html", "Positions:").injectInside(maintext4);
var select_position = new Element('select',{'id':'jform_positions_tab_'+$numbs, 'name':'jform_positions_tab'}).injectInside(maintext4);
var maintext5 = new Element('div',{'class':'maintext'}).injectInside(itemadd_edit);
var textbutton = new Element('div',{'class':'textbutton'}).injectInside(maintext5);
var spbuttonsv = new Element('span',{'class':'custombtn jform_save', 'onclick':'upgradeItem('+$numbs+')'}).set("html","Save").injectInside(textbutton);
var spbuttoncan = new Element('span',{'class':'custombtn jform_cancel', 'onclick':'cancelItem('+$numbs+')'}).set("html","Cancel").injectInside(textbutton); 
if(content_type=='module'){
var input_db = new Element('input',{'value':title+'[]'+content_type+'[]'+position, 'type':'hidden', 'class':'hiddendata', 'id':'hiddendata_'+$numbs}).injectInside(container_edit);
}else{
var input_db = new Element('input',{'value':title+'[]'+content_type+'[]'+content, 'type':'hidden', 'class':'hiddendata', 'id':'hiddendata_'+$numbs}).injectInside(container_edit);
} 
var numbsitem = new Element('input',{'value':$numbs, 'type':'hidden', 'class':'numbsitem'}).injectInside(container_edit); 

for(var i=0; i < $options.length-1; i++) { 
var opt       = document.createElement('option');
opt.value     = $options[i]; 
opt.innerHTML = $options[i]; 
$('jform_positions_tab_'+$numbs).appendChild(opt); 
}
for(k = 0; k < $('jform_content_type_tab').options.length; k++) {
var opttype       = document.createElement('option');
opttype.value     = $('jform_content_type_tab').options[k].value; 
opttype.innerHTML = $('jform_content_type_tab').options[k].innerHTML; 
$('jform_content_type_tab_'+$numbs).appendChild(opttype); 
}
for(n = 0; n < $('jform_positions_tab_'+$numbs).options.length; n++) { 
if($('jform_positions_tab_'+$numbs).options[n].value == position) {
$('jform_positions_tab_'+$numbs).options[n].selected = true;
}
}
for(m = 0; m < $('jform_content_type_tab_'+$numbs).options.length; m++) { 
if($('jform_content_type_tab_'+$numbs).options[m].value == content_type) {
$('jform_content_type_tab_'+$numbs).options[m].selected = true;
}
} 
var changeContentType = function(type,wait){
if(wait){
if(type == 'module'){
$('jform_custom_content_tab_'+($numbs-1)).getParent().setStyle('display','none');
$('jform_positions_tab_'+($numbs-1)).getParent().setStyle('display','');
}else if(type == 'custom'){
$('jform_custom_content_tab_'+($numbs-1)).getParent().setStyle('display','');
$('jform_positions_tab_'+($numbs-1)).getParent().setStyle('display','none');
}
}else{
if(type == 'module'){
$('jform_custom_content_tab_'+$numbs).getParent().setStyle('display','none');
$('jform_positions_tab_'+$numbs).getParent().setStyle('display','');
}else if(type == 'custom'){
$('jform_custom_content_tab_'+$numbs).getParent().setStyle('display','');
$('jform_positions_tab_'+$numbs).getParent().setStyle('display','none');
}
} 
}; 	 
$('jform_content_type_tab_'+$numbs).addEvent('change',function(){
changeContentType(this.value,true);                
});
changeContentType(content_type);
var art_id = $('jform_params_tabs_data'); 
var st_id = art_id.value;
if(content_type=='module'){
st_id = st_id + title +'[]'+ content_type + '[]' + position +'||'; 
}else{
st_id = st_id + title +'[]'+ content_type + '[]' + content + '||'; 
}

art_id.innerHTML = st_id; 
fireEvent('sortingready'); 
customform_btns[1].fireEvent('click'); 
}else{
alert('Please insert data');	
} 
}else{
var checktype = $('hiddendata_'+$numbs).value.split('[]');
var strrep = htmlspecialchars_decode($('jform_custom_content_tab_'+$numbs).value);
var art_id = $('jform_custom_content_tab_'+$numbs);
art_id.innerHTML=strrep;
if(checktype[1]=='module'){ 
$('jform_content_type_tab_'+$numbs).selectedIndex=0;
$('jform_custom_content_tab_'+$numbs).getParent().setStyle('display','none');
$('jform_positions_tab_'+$numbs).getParent().setStyle('display','');
}
if(checktype[1]=='custom'){
$('jform_content_type_tab_'+$numbs).selectedIndex=1;
$('jform_custom_content_tab_'+$numbs).getParent().setStyle('display','');
$('jform_positions_tab_'+$numbs).getParent().setStyle('display','none');
}
var ContentType = function(type,numb){ 
if(type == 'module'){
$('jform_custom_content_tab_'+numb).getParent().setStyle('display','none');
$('jform_positions_tab_'+numb).getParent().setStyle('display','');
}else if(type == 'custom'){
$('jform_custom_content_tab_'+numb).getParent().setStyle('display','');
$('jform_positions_tab_'+numb).getParent().setStyle('display','none');
} 
};
$$('.jform_content_type_tab').each(function(e,i){ 
e.addEvent('change', function(){
ContentType(this.value,i);	
});
});
}
$numbs ++;
}
$$('div#custom_list div.custom_item').each(function(e,i){ 
addItem(i);
});
});  
function upgradeItem(numbs){ 
var title = $('jform_title_tab_'+numbs).value;
var content_type = $('jform_content_type_tab_'+numbs).value;
var content = htmlspecialchars($('jform_custom_content_tab_'+numbs).value);
var position = $('jform_positions_tab_'+numbs).value;
if(content_type=='module'){
var hiddendata = $('hiddendata_'+numbs).value=title+'[]'+content_type+'[]'+position;
}else{
var hiddendata = $('hiddendata_'+numbs).value=title+'[]'+content_type+'[]'+content;
}
var element = $('hiddendata_'+numbs).getParent().getParent(); 
element.getElement('.custom_item_title').set('html',title);
element.getElement('.type').set('html',content_type);
var scroller = element.getElement('.content-form-edit');
scroller.setStyle('height', scroller.getSize().y + "px");
new Fx.Tween(scroller, { duration: 300, property: 'height' }).start(0); 

var art_id = $('jform_params_tabs_data');
var datazoro = ''; 
$$('#custom_list input.hiddendata').each(function(item,i){
datazoro = datazoro +  item.value  + '||';
});
art_id.innerHTML = datazoro; 
}
function cancelItem(numbs){ 
$$('div#custom_list div.custom_item').each(function(e,i){ 
if(e.getElement('.numbsitem').value==numbs){
var scroller = e.getElement('.content-form-edit');
scroller.setStyle('height', scroller.getSize().y + "px");
new Fx.Tween(scroller, { duration: 300, property: 'height' }).start(0);
}
});
}
function htmlspecialchars(str) {
if (typeof(str) == "string") {
str = str.replace(/&/g, "[ztamp]");
str = str.replace(/"/g, "[ztdquote]");
str = str.replace(/'/g, "[ztquote]");
str = str.replace(/</g, "[ztlt]");
str = str.replace(/>/g, "[ztgt]");
}
return str;
}
function htmlspecialchars_decode(str) {
if (typeof(str) == "string") {
str = str.replace(/\[ztgt\]/g, ">");
str = str.replace(/\[ztlt\]/g, "<");
str = str.replace(/\[ztquote\]/g, "'");
str = str.replace(/\[ztdquote\]/g, '"');
str = str.replace(/\[ztamp\]/g, '&');
}
return str;
}
window.addEvent('domready', function(){			
fireEvent('sortingready');
}); 
window.addEvent('sortingready', function(){ 
$$('#custom_list .remove').addEvent('click', function(element){ 
if(element) element.stop();
var container = $(this).getParent().getParent();
container.dispose();
var allid = $$('#custom_list input.hiddendata'); 
var art_id = $('jform_params_tabs_data');  
var st_id = '';  
for (var i = allid.length-1; i >= 0; i--) { 
st_id = st_id + allid[i].value + '||'; 
} 
art_id.innerHTML = st_id; 
});
$$('#custom_list a.edit').each(function(element,i){
element.addEvent('click', function(item){
var container = $(this).getParent().getParent();
if(item) item.stop();
var scroller = container.getElement('.content-form-edit'); 
scroller.setStyle('height', scroller.getSize().y + "px");
var fx = new Fx.Tween(scroller, { duration: 300, property: 'height', onComplete: function() { if(scroller.getSize().y > 0) scroller.setStyle('height', 'auto'); } });

if(scroller.getSize().y > 0) {
fx.start(0);
} else {
var items = container.getParent().getElements('.custom_item'); 
items.each(function(element) { 
if(element != container) element.getElements('span.custombtn')[1].fireEvent('click');  
}); 
fx.start(scroller.getElement('div').getSize().y); 
}  
});
});  
if (typeof SJdrapdrop != 'undefined') {
new SJdrapdrop(
$$("#custom_list"),{classmove: '.move'})
};
});
</script> 
<?php return $html_return; 
} 
}  